package com.johnny.cms.core.controller.nob;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.johnny.cms.core.domain.blog.Post;
import com.johnny.cms.core.service.CmsBlogService;
import com.johnny.core.hibernate.orm.Page;

@Controller
public class RssController {
	@Autowired
	private CmsBlogService cmsBlogService;

	@RequestMapping(value = "/rssfeed", method = RequestMethod.GET)
	public ModelAndView getFeedInRss() {

		List<SampleContent> items = new ArrayList<SampleContent>();
		Page<Post> page = new Page<Post>(10);
		page.setOrder("asc");
		page.setOrderBy("postDate");
		cmsBlogService.findPostByPage(page, null);
		for (Post post : page.getResult()) {
			SampleContent content = new SampleContent();
			content.setTitle(post.getTitle());
			content.setUrl(cmsBlogService.getBlogVisitURl(post));
			content.setSummary(post.getSummary());
			content.setCreatedDate(post.getPostDate());
			items.add(content);
		}

		ModelAndView mav = new ModelAndView();
		mav.setViewName("rssViewer");
		mav.addObject("feedContent", items);
		return mav;
	}
}
